package com.yld.car.market;

import java.util.HashMap;

import com.yld.car.common.ConstantUtils;
import com.yld.car.common.NetworkProgressUtils;

import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ForgetActivity extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.retrieve_password);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
		showBackButton();
		((TextView) findViewById(R.id.title)).setText("找回密码");

		telephoneText = (EditText) findViewById(R.id.telephone);

		telephoneText.addTextChangedListener(textWatcherOne);
		mInputCheckCodeText = (EditText) findViewById(R.id.inputCheckCode);
		mInputCheckCodeText.addTextChangedListener(textWatcherTwo);
		mGetCheckCodeBtn = (Button) findViewById(R.id.getCheckCode);

		mSubmitBtn = (Button) findViewById(R.id.submit);

		utils = NetworkProgressUtils.getInstance();
	}

	private EditText telephoneText;
	private Button mGetCheckCodeBtn;
	private Button mSubmitBtn;
	private EditText mInputCheckCodeText;
	TextWatcher textWatcherOne = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {

			if (s.toString().trim().length() == 11) {

				mGetCheckCodeBtn.setEnabled(true);
			} else {
				mGetCheckCodeBtn.setEnabled(false);
			}

		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO Auto-generated method stub

		}

		@Override
		public void afterTextChanged(Editable s) {

		}
	};

	TextWatcher textWatcherTwo = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {

			if (s.toString().trim().length() == 4) {

				mSubmitBtn.setEnabled(true);
			} else {
				mSubmitBtn.setEnabled(false);
			}

		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO Auto-generated method stub

		}

		@Override
		public void afterTextChanged(Editable s) {
			// TODO Auto-generated method stub

		}
	};
	private NetworkProgressUtils utils = null;

	public void onAction(View v) {

		int id = v.getId();

		switch (id) {
		case R.id.getCheckCode:
			String number = telephoneText.getText().toString();

			HashMap<String, String> allParams = new HashMap<String, String>();
			allParams.put("telphone", number);
			// System.out.println(number + "....number=");
			new ForgetTask(allParams, 29, "手机验证码已经发送到您的手机，请注意查收！",
					"您输入的手机号码不存在，请确认后再输入！", mInputCheckCodeText)
					.execute(ConstantUtils.GET_DEALER_PASS_INFO_URL);
			break;

		case R.id.submit:

			String checkCodeText = mInputCheckCodeText.getText().toString();
			System.out.println(checkCodeText);
			allParams = new HashMap<String, String>();
			allParams.put("checkcode", checkCodeText);
			new ForgetTask(allParams, 30, "重置后的密码已经发送到您的手机，请注意查收！",
					"验证码错误，请先确认！", null)
					.execute(ConstantUtils.GET_DEALER_PASS_INFO_TWO_URL);
			break;
		}
	}

	private class ForgetTask extends AsyncTask<String, String, String> {

		public ForgetTask(HashMap<String, String> allParams, int methodIndex,
				String success, String failure, EditText e) {

			this.allParams = allParams;
			this.methodIndex = methodIndex;
			this.success = success;
			this.failure = failure;
			this.e = e;
		}

		private HashMap<String, String> allParams = null;
		private int methodIndex;
		private String success;
		private String failure;
		private EditText e;

		@Override
		protected String doInBackground(String... params) {

			/**
			 * 
			 * <telphone>string</telphone> </GetDealerPassInfo>
			 */
			// HashMap<String, String> allParams = new HashMap<String,
			// String>();
			//
			// allParams.put("telephone", params[0]);
			publishProgress("");
			Object o = utils.getWebservicesByData(
					ConstantUtils.NAME_SPACE,
					ConstantUtils.ALL_METHROD_NAME.get(methodIndex),
					ConstantUtils.NAME_SPACE
							+ ConstantUtils.ALL_METHROD_NAME.get(methodIndex),
					params[0], allParams);

			System.out.println(o);
			return o.toString();
		}

		@Override
		protected void onPostExecute(String result) {

			hideLoading();
			if (result != null && result.indexOf("已经发送") >= 0) {

				if (e != null) {
					e.setEnabled(true);
				}
				Toast.makeText(ForgetActivity.this, success, Toast.LENGTH_SHORT)
						.show();

			} else {
				Toast.makeText(ForgetActivity.this, failure, Toast.LENGTH_SHORT)
						.show();
			}
			super.onPostExecute(result);
		}

		@Override
		protected void onProgressUpdate(String... values) {

			showLoading("正在获取......");
			super.onProgressUpdate(values);
		}

	};
}
